home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6450 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: admaix.sunydutchess.edu!ub!newserve!rebecca!rpi!not-for-mail
  2. From: floydb1@lib108.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q. only for ostrstream experts!!!
  5. Date: 8 Feb 1996 09:21:25 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Distribution: world
  8. Message-ID: <4fd0t5$dj2@lib108.its.rpi.edu>
  9. References: <4fab9d$1rp@fsuj01.rz.uni-jena.de> <ENNO.96Feb7205043@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  10. NNTP-Posting-Host: lib108.its.rpi.edu
  11. X-newsreader: xrn 7.04-beta-11
  12.  
  13.  
  14. In article <ENNO.96Feb7205043@kitz.inferenzsysteme.informatik.th-darmstadt.de>, enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) writes:
  15. |> In article <4fab9d$1rp@fsuj01.rz.uni-jena.de> mkt@isun04.inf.uni-jena.de (Tilo Koerbs) writes:
  16. |> 
  17. |>    Consider:
  18. |>        ostrstream buf();  // dynamic buffer
  19. |>        buf << "something" << ends;
  20. |>        char *p = buf.str();  // freeze buffer and return a pointer to it
  21. |> 
  22.  
  23. -------------------------------------------------
  24.  
  25. String    SomeFunction ( int some_data )
  26. {
  27.     ostrstream     this_stream ;
  28.     String         this_string ;
  29.  
  30.     this_stream     << some_data
  31.             << ends ;
  32.  
  33.     this_string = this_stream.str () ;
  34.  
  35.     this_stream.freeze ( 0 ) ;
  36.  
  37.     return ( this_string ) ;
  38. }
  39.  
  40. -------------------------------------------------
  41.  
  42. I use a String class, eliminating many problems
  43. associated with managing ptr's. None-the-less,
  44. the '.freeze ( 0 )' statement will allow the
  45. underlying buf to be destroyed. In the above
  46. case, upon exit from the function as it goes out
  47. of scope.
  48.  
  49. Your ptr to that buffer would then be deleted
  50. with 'delete [] p'. This works for me, in
  51. practice, though I will not claim to be an
  52. expert.
  53.  
  54. barry
  55.  
  56. ps
  57.  
  58. this is one way to convert any type of data to
  59. a 'char*' or 'String'.
  60. -- 
  61. +--------------------------------------------------------------------+ 
  62. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  63. | RPI Alum. '84 '87 '88              \\\                             |
  64. +--------------------------------------------------------------------+
  65.